<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class penj extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->library('form_validation');
	}

	public function index(){
		$this->load->model('penjualan','',true);
		$this->load->model('barang','',true);
		$this->load->model('karyawan','',true);
		$this->load->model('konsumen','',true);
		$this->load->model('pengiriman','',true);
		
		$data['penjualan'] = $this->penjualan->get_all();
		
		$data['pengiriman'] = $this->pengiriman->get_allFilter();
		$data['pelanggan'] = $this->konsumen->get_all();
		$data['karyawan'] = $this->karyawan->get_all();
		$data['barang'] = $this->barang->get_all();
		
		$id_penjualan=$this->penjualan->maksid()->row()->maksid+1;
		
		$data['no_penjualan']='penj_bw_'.$id_penjualan;
		$this->load->view('header');
		$this->load->view('menu');
		$this->load->view('penjualan',$data);
		$this->load->view('footer');
	}

	public function insert(){
	
	$this->load->model('penjualan','',true);
	$id_penjualan=$this->penjualan->maksid()->row()->maksid+1;
	
		$data = array(
				'id_penjualan' 			=> $id_penjualan,
				'no_penjualan' 			=> 'penj_bw_'.$id_penjualan,
				'tanggal'				=> date('d-m-Y'),
				'no_pengiriman'			=> $this->penjualan->getPropertiesPenjualan($this->input->post('no_pengiriman'))->row()->no_pengiriman,
				'nama' 					=> $this->penjualan->getPropertiesPenjualan($this->input->post('no_pengiriman'))->row()->nama_konsumen,
				'alamat' 				=> $this->penjualan->getPropertiesPenjualan($this->input->post('no_pengiriman'))->row()->alamat_konsumen,
				'karyawan'				=> $this->penjualan->getPropertiesPenjualan($this->input->post('no_pengiriman'))->row()->nama_karyawan,
				'jenis_barang'			=> $this->penjualan->getPropertiesPenjualan($this->input->post('no_pengiriman'))->row()->nama_barang,
				'angsuran'				=> $this->penjualan->getPropertiesPenjualan($this->input->post('no_pengiriman'))->row()->angsuran,
				'jenis_pembayaran'		=> $this->penjualan->getPropertiesPenjualan($this->input->post('no_pengiriman'))->row()->jenis_pembayaran,
				'kuantitas'				=> $this->penjualan->getPropertiesPenjualan($this->input->post('no_pengiriman'))->row()->jumlah,
				'harga_satuan'			=> $this->penjualan->getPropertiesPenjualan($this->input->post('no_pengiriman'))->row()->harga_satuan,
				'potongan_penjualan'	=> $this->input->post('potongan_penjualan'),
				'jumlah'				=> $this->input->post('jumlah')
			);
		$this->load->model('penjualan','',true);
		$this->form_validation->set_rules('nama', 'nama', 'trim|required|is_unique[aktiva.nama]');
		
		$this->penjualan->insert($data);
		
		$this->load->model('pengiriman','',true);
		$no_pengiriman=$this->penjualan->getPropertiesPenjualan($this->input->post('no_pengiriman'))->row()->no_pengiriman;
		
		$data = array(
				'status_pengiriman' 			=> '2'
				
			);
		$this->pengiriman->update($no_pengiriman,$data);
	
			
			
			redirect('penj');
			
		
	}

	public function delete($data){
		$this->load->model('penjualan','',true);
		$this->penjualan->delete($data);
		redirect('penj');
	}

	public function edit($id){
		$this->load->model('penjualan','',true);
		$data['detail'] = $this->penjualan->get_by_id($id);
		$data['penjualan'] = $this->penjualan->get_all();
		$this->load->view('header');
		$this->load->view('menu');
		$this->load->view('penjualan',$data);
		$this->load->view('footer');
	}
	
	public function update(){
		$data = array(
				'no_aktiva' 			=> $this->input->post('no_aktiva'),
				'nama' 					=> $this->input->post('nama'),
				'keterangan' 			=> $this->input->post('keterangan'),
				'harga_perolehan'		=> $this->input->post('harga_perolehan'),
				'nilai_residu'			=> $this->input->post('nilai_residu'),
				'umur_ekonomis'			=> $this->input->post('umur_ekonomis'),
				'cabang'				=> $this->input->post('cabang')
			);
		$id = $this->input->post('no_aktiva');
		$this->load->model('penjualan','',true);
		$this->form_validation->set_rules('nama', 'nama', 'trim|required|is_unique[aktiva.nama]');
		if ($this->form_validation->run() == FALSE) {
			$menu['error'] = true;
			$this->load->model('penjualan','',true);
			$data['penjualan'] = $this->penjualan->get_all();
			$this->load->view('header');
			$this->load->view('menu');
			$this->load->view('penjualan',$data);
			$this->load->view('footer');
		} else {
			$this->penjualan->update($id,$data);
			redirect('penj');
		}
	}

	public function validate($id){
		$data = array(
				'status' 			=> '1'
				
			);

		$this->load->model('penjualan','',true);
		$this->form_validation->set_rules('nama', 'nama', 'trim|required|is_unique[aktiva.nama]');
	
			$this->penjualan->update($id,$data);
			$this->load->model('penjualan','',true);
			$this->penjualan->update($id,$data);
			
			
			//load tagihan
			 //$angsuran=$this->penjualan->get_by_id($id)->row()->angsuran;
			  $angsuran=2;
			$number=1;
			while($angsuran>0){
			$data = array(
				'nomer_tagihan' 			=> $this->input->post('no_penjualan'),
				'jenis_tagihan' 					=> $this->input->post('nama'),
				'tanggal' 			=> date('d-M-Y'),
				'no_penjualan'		=> $id,
				'angsuran_ke'		=> $number,
				
			);
			
			$this->load->model('penjualan','',true);
			$this->penjualan->insertagihan($data);
			$angsuran--;
			$number++;
			}
			
			redirect('penj');
		
	}

	
	
	
	}